function noisy_signal = add_awgn_to_cdma(signal, snr_dB)
% add_awgn_to_cdma - 向CDMA信号添加AWGN噪声
%
% 输入参数:
%   signal  - 输入CDMA信号
%   snr_dB  - 信噪比(dB)
%
% 输出参数:
%   noisy_signal - 添加噪声后的信号

% 计算信号功率
signal_power = mean(abs(signal).^2);

% 将SNR从dB转换为线性比例
snr_linear = 10^(snr_dB/10);

% 计算所需的噪声功率
noise_power = signal_power / snr_linear;

% 生成复高斯白噪声
noise = sqrt(noise_power/2) * (randn(size(signal)) + 1i*randn(size(signal)));

% 添加噪声到信号
noisy_signal = signal + noise;

% 添加多径效应 (简化模拟)
multipath_simulation = true;
if multipath_simulation
    % 创建简单的多径信道
    path_delays = [0, 5, 15];              % 路径延迟(芯片)
    path_gains_dB = [0, -3, -10];          % 路径衰减(dB)
    path_gains = 10.^(path_gains_dB/10);    % 转换为线性比例
    
    % 应用多径效应
    multipath_signal = zeros(size(noisy_signal));
    
    for i = 1:length(path_delays)
        delayed_signal = [zeros(1, path_delays(i)) noisy_signal(1:end-path_delays(i))];
        multipath_signal = multipath_signal + path_gains(i) * delayed_signal;
    end
    
    % 归一化多径信号功率
    multipath_signal = multipath_signal * sqrt(signal_power / mean(abs(multipath_signal).^2));
    
    % 将多径信号作为最终的接收信号
    noisy_signal = multipath_signal;
end

end 